
--由callcenter_config queue list members  命令产生的坐席列表
--local sall='queue|system|uuid|session_uuid|cid_number|cid_name|system_epoch|joined_epoch|rejoined_epoch|bridge_epoch|abandoned_epoch|base_score|skill_score|serving_agent|serving_system|state|score\nnecoagent|single_box|34b2e88e-44a9-11e9-b062-0f781f422f30|2ac733ca-44a9-11e9-b059-0f781f422f30|18995555220|18995555220|1552382896|1552382913|0|0|0|17|0|||Waiting|91\nnecoagent|single_box|405ac7b0-44a9-11e9-b06b-0f781f422f30|36746da0-44a9-11e9-b063-0f781f422f30|1002|1002|1552382916|1552382933|0|0|0|17|0|||Waiting|71necoagent|single_box|34b2e88e-44a9-11e9-b062-0f781f422f30|2ac733ca-44a9-11e9-b059-0f781f422f30|18995555220|18995555220|1552382896|1552382913|0|0|0|17|0|||Waiting|91';



--取出脚本的第一个参数
local ignoreA=argv[1];

if(ignoreA== nil)then
	return "beeak";
end



--获取所有在线的用户
local api=freeswitch.API();
local queuename="necoagent";
sall=api:executeString("callcenter_config  queue list  members "..queuename);



registersline=api:executeString("show registrations");


local userlines={};
string.gsub(registersline, '[^\n]+', function(w) table.insert(userlines, w) end );

if(userlines[1] ==nil)then
	return "break";
end

--取出返回命令的第一列，第一列是在线分机账号，然后保存到table，用于最后循环发送给在线分机消息
local onlineusers={};
for i=2,#(userlines)do
	local index,len= string.find(userlines[i],"[^,]+");
	local rindex=string.find(userlines[i],",");
	if(index and rindex) then
		local uname=string.sub(userlines[i],index,len);
		freeswitch.consoleLog("info","uname->"..uname.."uline->"..userlines[i]);
		onlineusers[uname]=uname;
	end
end




--下面是获取队列的member列表
local lines={}
string.gsub(sall, '[^\n]+', function(w) table.insert(lines, w) end )
if(lines[1]==nil) then
	return;
end
local rhead={};
local head={};
string.gsub(lines[1], '[^|]+', function(w) table.insert(head, w) end )

for  k,v in pairs(head) do
	rhead[v]=k;
end


local nowline={}
local jsonresult='[';


--循环每一行,按照|分割得到每一个字段的值
--得到一个json数组形式的member 成员列表
for i=2,#(lines) do
	
	lines[i]=string.gsub(lines[i],"||","|NULL|");
	lines[i]=string.gsub(lines[i],"||","|NULL|");--保证index对齐

	freeswitch.consoleLog("info","line->"..lines[i]);
	string.gsub(lines[i], '[^|]+', function(w) table.insert(nowline, w) end )
	
	local cidindex=rhead["cid_number"];
	local sindex=rhead["state"];
	if(sindex and nowline[sindex]=="Waiting" and  cidindex and nowline[cidindex]) then
		jsonresult=jsonresult..","..nowline[cidindex];
	end

	--清空
	for k,v in pairs(nowline)do
		nowline[k]=nil;
	end
	
	nowline={};
	
end

jsonresult= jsonresult.."]";
jsonresult=string.gsub(jsonresult,"[[],","[");




--取出当前domain,用于发送消息
local domain=freeswitch.getGlobalVariable("domain");
freeswitch.consoleLog("info","--准备执行chat-----------"..jsonresult..domain);
for k,v in pairs(onlineusers)
do
	--if(v ~= ignoreA)then --过滤当前正在呼入的分机，不能给这个分机发送message，否则会卡住
		freeswitch.consoleLog("info","----sent"..v);

	 	--api:executeString("chat sip|1000|"..v.."@"..domain.."|"..jsonresult);
		--所有发送sms消息均为fire一个事件，不能直接chat
		local event = freeswitch.Event("CUSTOM", "SMS::SEND_MESSAGE");
		   event:addHeader("proto", "sip");
		   event:addHeader("dest_proto", "sip");
		   event:addHeader("from", "1000");
		   event:addHeader("from_full", "sip:1000@"..domain)
		   event:addHeader("to",v.."@"..domain);--接收者
		   event:addHeader("subject", "sip:"..v.."@"..domain);
		   event:addHeader("sip_profile", "internal");
	   event:addBody("{\"type\":\"QUEUE_CHANGE\",\"data\":\""..jsonresult.."\"}");
--		   [18995555220,1002,1002,1002,1002,1002,1002,18995555220,1002,1002,1002,1002,1002,1002,18995555220,1002,1002,1002,1002,1002,1002,18995555220,1002,1002,1002,1002,1002,1002i
		  --  event:addBody("{\"type\":\"QUEUE_CHANGE\",\"data\":\"[18995555220,1002,1002,1002,1002,1002,1002,18995555220,1002,1002,1002,1002,1002,1002,18995555220,1002,1002,1002,1002,1002,1002,18995555220,1002,1002,1002,1002,1002,1002]\"}");		   
	       	   event:fire();
	--end
end
freeswitch.consoleLog("info","end----------->");
